package hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Brand;
import pojo.Products;
import dao.BrandDAO;

@Repository("BrandDAO")
public class HibernateBrandDAO implements BrandDAO {

private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Transactional
	public List<Brand> getBrandList() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Brand");
		return query.list();
	}
	
	@Transactional
	public Brand getBrand(String brandid) {
		Session session = sessionFactory.getCurrentSession();
        Brand br = null;
        String hql = String.format("from Brand br where br.brandid= %s", brandid);
        Query query = session.createQuery(hql);
        List<Brand> listbr = query.list();
        if(listbr.size() == 1)
            br = listbr.get(0);
        return br;
	}
}
